﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using StartEAS_API.Utils;
using StartsEAS_API.BLL.Interfaces.IUserB;
using StartsEAS_API.Model;

namespace StartsEAS_API.Controllers.UserContor
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class UserController : ControllerBase
    {
        private readonly IUserBLL _userBll;
        public UserController(IUserBLL userBll) { 
            _userBll = userBll;
        }
        /// <summary>
        /// 获取所有用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetAllUser() {
            try
            {
                return _userBll.GetAllUser();
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetAllUser服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="data">User实体JSON</param>
        /// <returns></returns>
        [HttpPost]
        public ResultUtils InsertUser(JObject data) {
            try
            {
                return _userBll.InsertUser(data);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"InsertUser服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="oldPwd">用户旧密码</param>
        /// <param name="newPwd">用户新密码</param>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        [HttpPut]
        public ResultUtils UpdatePassword(string oldPwd, string newPwd, string userName) {
            try
            {
                return _userBll.UpdatePassword(oldPwd, newPwd, userName);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"UpdatePassword服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 忘记密码
        /// </summary>
        /// <param name="idcard">身份证号码</param>
        /// <param name="newPwd">新密码</param>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        [HttpPut]
        public ResultUtils ForgotPassword(string idcard, string newPwd, string userName)
        {
            try {
                return _userBll.ForgotPassword(idcard, newPwd, userName);
            } catch (System.Exception e) {
                AppLog.WriteError($"ForgotPassword服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
    }
}
